import Cart from '@/views/cart.vue'
import Cla from '@/views/cla.vue'
import Detail from '@/views/detail.vue'
import Goods from '@/views/goods.vue'
import Home from '@/views/home.vue'
import Index from '@/views/index.vue'
import Login from '@/views/login.vue'
import My from '@/views/my.vue'
import Register from '@/views/register.vue'
import Search from '@/views/search.vue'
import { createRouter, createWebHistory } from 'vue-router'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/', redirect: '/home/index' },
    {path: '/detail/:goods_id',name: 'detail',component: Detail},
    {path: '/goods/:query',name: 'goods',component: Goods},
    {path: '/search',name: 'search',component: Search},
    {path: '/register',name: 'register',component: Register},
    {path: '/login',name: 'login',component: Login},
    {path: '/home',name: 'home',component: Home, children:[
      {path: 'index',name: 'index',component: Index},
      {path: 'cla',name: 'cla',component: Cla},
      {path: 'cart',name: 'cart',component: Cart},
      {path: 'my',name: 'my',component: My},
    ]},
  ],
})

export default router
router.beforeEach((to,from,next)=>{
    if(to.fullPath==='/home/my'||to.fullPath==='/home/cart'){
      let token=localStorage.getItem('token')
      if(!token){
        next('/login')
      }else{
        next()
      }
    }else{
      next()
    }
})